<template>
  <div>
    <h2>当前和为：{{ sum }}</h2>
    <h2>当前和放大10倍：{{ bigSum }}</h2>
    <h2>我是{{ name }},今年{{ age }}</h2>
    <select v-model.number="n">
      <option :value="1">1</option>
      <option :value="2">2</option>
      <option :value="3">3</option>
    </select>
    <button @click="increment(n)">++</button>
    <button @click="decrement(n)">--</button>

    <button @click="incrementOdd(n)">当前和为奇数再加</button>
    <button @click="decrementOdd(n)">等2秒再加</button>
  </div>
</template>

<script>
import { mapGetters, mapState, mapMutations, mapActions } from "vuex";
export default {
  name: "Count",
  data() {
    return {
      n: 1,
    };
  },
  computed: {
    // 借助mapState生成计算属性，从state读取数据，（对象方法
    // ...mapState({ sum1: "sum", name1: "name", age1: "age" }),
    // 借助mapState生成计算属性，从state读取数据，（数组方法
    ...mapState(["sum", "name", "age"]),

    // 借助mapGetters生成计算属性，从getters读取数据，（对象方法
    // ...mapGetters({ bigSum: "bigSum" }),
    // 借助mapGetters生成计算属性，从getters读取数据，（数组方法
    ...mapGetters(["bigSum"]),
  },
  methods: {
    // increment() {
    //   this.$store.commit("PLUS", this.n);
    // },
    // decrement() {
    //   this.$store.dispatch("reduce", this.n);
    // },
    // 借助mapMutations生成对应的方法，方法中会调用commit去联系mapMutations（对象
    ...mapMutations({ increment: "PLUS", decrement: "REDUCE" }),
    // 借助mapMutations生成对应的方法，方法中会调用commit去联系mapMutations（对象
    ...mapMutations(["PLUS", "REDUCE"]),

    // incrementOdd() {
    //   if (this.$store.state.sum % 2) {
    //     this.$store.dispatch("plusOdd", this.n);
    //   }
    // },
    // decrementOdd() {
    //   setTimeout(() => {
    //     this.$store.dispatch("plusWait", this.n);
    //   }, 2000);
    // },

    // 借助mapActions生成对应的方法，方法中会调用commit去联系mapActions（对象
    ...mapActions({ incrementOdd: "plusOdd", decrementOdd: "plusWait" }),
    // 借助mapActions生成对应的方法，方法中会调用commit去联系mapActions（数组
    ...mapActions(["plusOdd", "plusWait"]),
  },
  mounted() {},
};
</script>

<style lang="css">
button {
  margin-left: 5px;
}
</style>